package com.sdzee.dao;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.*;

import model.Event;

@Stateless
public class EventDaoEJB {
	//Injection du Manager qui s'occupe de la connexion avec la BDD
	@PersistenceContext (unitName = "mydb_PU")
	private EntityManager em;
	
	public void update(Event oldEvent, Event NewEvent) throws DAOException {
		try{
			em.persist(NewEvent);     
			em.remove(oldEvent);
		} catch(Exception e){throw new DAOException (e);}
		}
	

	public void deleteEvent(int eventId) throws DAOException {
		
		
		Query query = em.createQuery("delete from Event e where e.idevent=:p");
		query.setParameter("p", eventId).executeUpdate();

		}
	
	public void add(Event event) throws DAOException {
		try{			
			em.persist(event);

		} catch(Exception e){throw new DAOException (e);}
		}
	
	@SuppressWarnings("unchecked")
	public boolean notExistEventName( String name){
		
		ArrayList<Event> list = new ArrayList<Event>() ;
		Query q = em.createQuery("select e from Event e where e.name=:N");
		q.setParameter("N", name);
		try{
			list = (ArrayList<Event>) q.getResultList();
			return list.isEmpty();
		} catch(Exception e){throw new DAOException (e);}
	}
	
	@SuppressWarnings("unchecked")
	public List<Event> findEventsCreator(String pseudoCreator){
		
		List<Event> list = new ArrayList<Event>() ;
		Query q = em.createQuery("select e from Event e where e.eventCreator=:N");
		q.setParameter("N", pseudoCreator);
		try{
			list = (List<Event>) q.getResultList();
			return list;
		} catch(Exception e){throw new DAOException (e);}
	}
	
	@SuppressWarnings("unchecked")
	public List<Event> findEventsForTheInvited(String eventAbout){
		List<Event> list = new ArrayList<Event>() ;
		Query q = em.createQuery("select e from Event e where e.name=:N");
		q.setParameter("N", eventAbout);
		try{
			list = (List<Event>) q.getResultList();
			return list;
		} catch(Exception e){throw new DAOException (e);}
	}
	
	public List<Event> selectAll() {
        List<Event> events = em.createQuery("select e from Event e").getResultList();
        return events;
    }
	
	public Event getEventById(int eventId){
		
		Query q = em.createQuery("select e from Event e where e.idevent=:N");
		q.setParameter("N", eventId);
		
		return (Event) q.getSingleResult();
	}
	
	
	public void updateEvent(Event event, int eventId){
		//em.merge(event);
		//String d = java.sql.Date(event.getDate().getTime());
		
		 Query query = em.createQuery("update Event e set e.date=:d, e.eventCreator=:ev, e.indoor_outdoor=:in, e.name=:na, e.place=:pa"+
		 " where e.idevent=:id");
		 
          Date d = 	new java.sql.Date(event.getDate().getTime());
          String ev = event.getEventCreator();
          String in = event.getIndoor_outdoor();
          String na = event.getName();
          String pa =  event.getPlace();
          
          
		  query.setParameter("d", d);
		  query.setParameter("ev", ev);
		  query.setParameter("in", in);
		  query.setParameter("na", na);
		  query.setParameter("pa", pa);
		  query.setParameter("id", eventId);
		  
		  query.executeUpdate();
	}
	}
